//
//  HMVisitorView.swift
//  SinaWeiBo
//
//  Created by immncjykeny on 2017/4/28.
//  Copyright © 2017年 immncjykeny. All rights reserved.
//

import UIKit

class HMVisitorView: UIView {
    
    //注册控件 类似于放在OC里面.h文件
    lazy var registerButton:UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    //登录控件
    lazy var loginButton:UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    //修改访客视图信息
    var visitorInfo:[String:String]?{
        didSet{
            guard let imageName = visitorInfo?["imageName"] ,
                let message = visitorInfo?["message"] else {
                    
                    return
            }
            //到这里一定有值了
            tipLabel.text = message
            
            //如果是首页 直接为空 返回
            if imageName == "" {
                startAnimation()
                return
            }
            
            //设置图片
            iconImage.image = UIImage(named: imageName)
            //小房子不需要在其他控制器显示
            houseIconImage.isHidden = true
            //遮罩也不需要显示
            maskIconImage.isHidden = true
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:设置访客信息
    func setupInfo(dict:[String:String]){
        
        
    
    }
    //MARK:设置首页转动动画
    func startAnimation()  {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * Double.pi
        animation.duration = 15
        animation.repeatCount = MAXFLOAT
        //为保证动画一直执行 而不至于退出 或者访问别的时候而中断 
        //当然当该控件iconImage删除 动画也就销毁了
        animation.isRemovedOnCompletion = false
        iconImage.layer.add(animation, forKey: nil)
    }
    //MARK:私有控件
    //圈圈控件
     fileprivate lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩控件
    fileprivate lazy var maskIconImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子控件
     fileprivate lazy var houseIconImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //字体标签
     fileprivate lazy var tipLabel:UILabel = UILabel.cz_label(withText: "关注一些人,这里有什么惊喜,关注一些人,这里有什么惊喜", fontSize: 14, color: UIColor.darkGray)
    
    
    
}
extension HMVisitorView{
    func setupUI(){
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
       
        addSubview(iconImage)
        addSubview(maskIconImage)
        addSubview(houseIconImage)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //设置文字居中
        
        tipLabel.textAlignment = .center
        
        //原生自动布局 需要关闭autolaysizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        let margin:CGFloat = 20
        //图像视图
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        //小房子视图
        addConstraint(NSLayoutConstraint(item: houseIconImage, attribute: .centerX, relatedBy: .equal, toItem: iconImage, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconImage, attribute: .centerY, relatedBy: .equal, toItem: iconImage, attribute: .centerY, multiplier: 1.0, constant: 0))
        
    // tip标签视图
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerX, relatedBy: .equal, toItem: iconImage, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute:.top, relatedBy: .equal, toItem: iconImage, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 236))
     //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: tipLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom , multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
      //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: tipLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: registerButton, attribute: .width, multiplier: 1.0, constant: 0))
        
        //使用VFL布局遮罩
        let viewDict = ["maskIconImage":maskIconImage,"registerButton":registerButton] as [String : Any]
        
        let metrics = ["spacing":-35]
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconImage]-0-|", options: [], metrics: nil, views: viewDict))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconImage]-(spacing)-[registerButton]", options: [], metrics: metrics, views: viewDict))
        
        
    }
}
